﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using StartEAS_API.Utils;
using StartsEAS_API.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace StartsEAS_API.Controllers.FilesContor
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    public class FilesController : ControllerBase
    {
        private readonly IHostingEnvironment _hostingEnvironment;

        public FilesController(IHostingEnvironment hostingEnvironment) {
            _hostingEnvironment = hostingEnvironment;
        }
        /// <summary>
        /// 下载Excel文件
        /// </summary>
        /// <param name="path">Excel名称</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<FileResult> DownloadExcel(string path)
        {
            System.Console.WriteLine("访问接口：DownloadExcel"+path);
            HttpContext.Response.ContentType = "application/xlsx";
            Stream stream = await Utils.GetExcel(path);
            var actionresult = new FileStreamResult(stream, new Microsoft.Net.Http.Headers.MediaTypeHeaderValue("application/xlsx"));
            actionresult.FileDownloadName = path + ".xlsx";
            return actionresult;
        }
        /// <summary>
        /// 上传文件，需要上传文件为Files
        /// </summary>
        /// <returns></returns>00
        [HttpPost]
        public ResultUtils UploadFile()
        {
            var files = Request.Form.Files;
            List<String> filenames = new List<string>();
            foreach (var file in files)
            {
                var fileName = Utils.ConvertDateTimeToInt(DateTime.Now) + file.FileName;
                fileName = _hostingEnvironment.WebRootPath + $"/UploadFile/{fileName}";
                filenames.Add(fileName);
                using (FileStream fs = System.IO.File.Create(fileName))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
            }
            return new ResultUtils { code = 200, message = Utils.ReadExcel(filenames[0]) };
        }
        /// <summary>
        /// 上传课程信息Excel表分析课程数据，需要上传Files
        /// </summary>
        /// <param name="path">未作使用</param>
        /// <returns></returns>
        [HttpPost]
        public ResultUtils GetSchedule(string path)
        {
            var files = Request.Form.Files;
            List<String> filenames = new List<string>();
            foreach (var file in files)
            {
                var fileName = Utils.ConvertDateTimeToInt(DateTime.Now) + file.FileName;
                fileName = _hostingEnvironment.WebRootPath + $"/UploadFile/{fileName}";
                filenames.Add(fileName);
                using (FileStream fs = System.IO.File.Create(fileName))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
            }
            return new ResultUtils() { code = 200, message = Utils.ReadSchedule(filenames[0]) };
        }


    }
}
